import { createRouter, createWebHashHistory, RouteRecordRaw, RouterOptions } from 'vue-router'
import HomeView from '@/views/home/HomeView.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const routerModules: Record<any, any> = import.meta.glob('./modules/*.router.ts', {
  eager: true
})

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home-view',
    component: HomeView
  }
]

// 模块自动导入
for (const routerModulesKey in routerModules) {
  routes.push(routerModules[routerModulesKey].default)
}

// 创建路由
const router = createRouter(<RouterOptions>{
  routes,
  history: createWebHashHistory()
})

// 全局前置守卫，在路由跳转开始时显示进度条
router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

// 全局后置守卫，在路由跳转结束时隐藏进度条
router.afterEach(() => {
  NProgress.done()
})

export { router }
